package com.bzxgc.sgwer.service.impl.role;

import com.bzxgc.sgwer.dao.role.RoleMenuMapper;
import com.bzxgc.sgwer.entity.role.Role;
import com.bzxgc.sgwer.entity.role.RoleMenu;
import com.bzxgc.sgwer.service.role.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    @Transactional
    public void addRoleMenuBatch(Long[] roleIds, Long[] menuIds) {
        roleMenuMapper.deleteRoleMenuByRoleIds(roleIds);
        List<RoleMenu> roleMenuList = new ArrayList<>();
        Date currentTime = new Date();
        for (Long roleId : roleIds) {
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenu.setAddTime(currentTime);
                roleMenuList.add(roleMenu);
            }
        }
        roleMenuMapper.addRoleMenuBatch(roleMenuList);
    }

    @Override
    public void deleteRoleMenuByRoleIds(Long[] roleIds) {
        roleMenuMapper.deleteRoleMenuByRoleIds(roleIds);
    }

    @Override
    public List<RoleMenu> getRoleMenuByRoleId(Long roleId) {
        return roleMenuMapper.getRoleMenuByRoleId(roleId);
    }

}
